package com.tomcat;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.HashMap;

public class HttpRequestAndResponse {
    private HashMap<String,HttpServlet> servletHashMap;

    public HttpRequestAndResponse(HashMap<String,HttpServlet> stringHttpServletHashMap){
        this.servletHashMap=servletHashMap;
    }
    public void httpAccepted(Socket socket) throws IOException {
        OutputStream outputStream = socket.getOutputStream();

        String ass="HTTP/1.1 202 Accepted\n" +
                "Date: Mon, 27 Jul 2009 12:28:53 GMT\n" +
                "Server: Apache\n" ;
        outputStream.write(ass.getBytes());
        outputStream.flush();
    }
    public void request(Socket socket) throws IOException {
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024*8];
        int len=0;
        StringBuilder stringBuilder = new StringBuilder();
        while (true){
            len=inputStream.read(bytes);
            System.out.println(new String(bytes,0,len));
            if (len<1024){
                break;
            }

        }
        HttpProtocol httpProtocol=parseHttpString(stringBuilder.toString());
        String httpProtocolUrl = httpProtocol.getUrl();
        HttpServlet httpServlet = servletHashMap.get(httpProtocolUrl);
        if ("Get".toUpperCase().equals(httpProtocol.getRequestMethod())){
            if(httpServlet!=null){
                httpServlet.doGet();
            }
        }
        if ("Post".toUpperCase().equals(httpProtocol.getRequestMethod())){
            if(httpServlet!=null){
                httpServlet.doPost();
            }
        }
    }
    public void response(Socket socket) throws IOException {
        OutputStream outputStream = socket.getOutputStream();
        String body="<html><head></head><body><h3>helloworld</h3></body></html>";
        String string="HTTP/1.1 200 OK\n" +
//                    "Location: http://www.baidu.com\n" +
                "Date: Mon, 27 Jul 2009 12:28:53 GMT\n" +
                "Server: Apache\n" +
                "Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT\n" +
                "ETag: \"34aa387-d-1568eb00\"\n" +
                "Accept-Ranges: bytes\n" +
                "Content-Length: "+ body.length()+"\n" +
                "Vary: Accept-Encoding\n" +
                "Content-Type: text/html" +
                "\n" +body;


        outputStream.write(string.getBytes());
        outputStream.flush();
    }
        private HttpProtocol parseHttpString(String http){
            HttpProtocol httpProtocol = new HttpProtocol();
            String[] strings = http.split("\n");
            System.out.println(strings);
            for (int i = 0,len=strings.length; i <len ; i++) {
                if (i==0){
                    String[] strings1 = strings[0].split(" ");
                    httpProtocol.setRequestMethod(strings1[0]);
                    httpProtocol.setUrl(strings1[1]);
                    String[] split = strings1[1].split("\\?");
                    httpProtocol.setServletUri(split[0]);
                }
            }

            return httpProtocol;
    }
}
